package com.example.demo.Controller;


import com.example.demo.bean.shangpin;
import com.example.demo.bean.shangpinDao;
import com.example.demo.entity.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;


@RestController
@CrossOrigin    //跨域请求
public class shangpinController {

    @Autowired
    shangpinDao sc;

    //单个数字
    @GetMapping("/helloword/{id}")
    public void helloword(@PathVariable("id") Integer id){
        System.out.println("helloword");
        System.out.println(id);
    }


    //查询全部
    @GetMapping("/listAll/{page}/{size}")
    public Page<shangpin> getAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        System.out.println("listAll");
        PageRequest request = PageRequest.of(page-1,size);
        return sc.findAll(request);
    }

    //直接提交一个对象，@RequestBody json转换成对象
    @PostMapping("/save")
    public String save(@RequestBody shangpin book){
        shangpin result = sc.save(book);
        if(result != null){
            return "success";
        }else{
            return "error";
        }
    }

    @GetMapping("/findclass/{name}/{status}")
    public List<shangpin> findclass(@PathVariable("name") String name,@PathVariable("status") Integer status){
        List<shangpin> result = sc.findByNameAndStatus(name,status);
        System.out.println(result.toArray());
        return result;
    }

    @GetMapping("/findclassname/{name}")
    public List<shangpin> findclassname(@PathVariable("name") String name){
        List<shangpin> result = sc.findByName(name);
        System.out.println(result.toArray());
        return result;
    }
    @GetMapping("/findclassstatus/{status}")
    public List<shangpin> findclassstatus(@PathVariable("status") Integer status){
        List<shangpin> result = sc.findByStatus(status);
        System.out.println(result.toArray());
        return result;
    }



    //删除数据库里的单条数据
    @GetMapping("/deleteOne/{id}")
    public String deleteOne(@PathVariable("id") Integer id) {
        sc.deleteById(id);
        return "删除成功";
    }

    //删除多条数据
    @GetMapping("/deleteAll")
    public String deleteAll() {
        sc.deleteAll();
        return "删除所有数据成功";
    }


    @GetMapping("/findAllById/{id}")
    public shangpin getSc(@PathVariable("id") Integer id) {
        return sc.findById(id).get();
    }


    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    private Resp<String> upload(@RequestParam("file") MultipartFile file){
        return this.upload1(file);
    }

    public Resp<String> upload1(MultipartFile file) {
        if(file.isEmpty()){
            return Resp.fail("400","文件为空!");
        }
        String OriginalFilename = file.getOriginalFilename();
        String fileName = System.currentTimeMillis()+"."+OriginalFilename.substring(OriginalFilename.lastIndexOf(".")+1);
        String filePath = "D:\\apache-tomcat-8.0.53-windows-x64\\apache-tomcat-8.0.53\\webapps\\ROOT\\image\\";
        File dest = new File(filePath+fileName);
        if(!dest.getParentFile().exists())
            dest.getParentFile().mkdirs();
        try {
            file.transferTo(dest);
        }catch (Exception e){
            e.printStackTrace();
            return Resp.fail("500",OriginalFilename+"上传失败！");
        }
        return Resp.success(fileName);
    }

}
